//
//  XYTopMenuView.swift
//  XYReadBook
//
//  Created by fox on 2017/9/9.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import UIKit

protocol XYTopMenuViewDelegate:NSObjectProtocol {
    
    func topmenuViewChangeSource(topMenuView:XYTopMenuView)//切换源
    
    func topmenuViewClose(topMenuView:XYTopMenuView) //关闭

    func topmenuViewRithItems(topMenuView:XYTopMenuView,rigthItems:UIButton)//右边Items
}

class XYTopMenuView: UIView {

    var bookName:String
    let closeBtn = UIButton() // 关闭
    let changeSourceBtn = UIButton() // 换源
    let titleLabel = UILabel()
    var rigthItems:UIButton = UIButton()
   weak var delegate:XYTopMenuViewDelegate?
    
    
     init(frame: CGRect,bookName:String) {
        self.bookName = bookName
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        backgroundColor = UIColor.black
        addSubview(changeSourceBtn)
        addSubview(closeBtn)
        addSubview(titleLabel)
        addSubview(rigthItems)
        titleLabel.text = bookName
        titleLabel.textColor = UIColor.white
        titleLabel.textAlignment = .center
        changeSourceBtn.setTitle("换源", for: .normal)

        closeBtn.setTitle("关闭", for: .normal)
        closeBtn.addTarget(self, action: #selector(close), for: .touchUpInside)
        changeSourceBtn.addTarget(self, action: #selector(changeSource), for: .touchUpInside)
        rigthItems.setImage(UIImage.init(named: "global_nav_more_white"), for: .normal)
        rigthItems.addTarget(self, action: #selector(clickRightItems), for: .touchUpInside)
        //
    }
    


    /// 右边itmes
    func clickRightItems() {
        XYLog(messsage: "点击了右边的items")
        delegate?.topmenuViewRithItems(topMenuView: self, rigthItems: rigthItems)
    }
    
    /// 切换源
    func changeSource () {
        delegate?.topmenuViewChangeSource(topMenuView: self)
    }
    
    /// 关闭
    func close () {
        delegate?.topmenuViewClose(topMenuView: self)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        closeBtn.frame = CGRect.init(x: 12, y: 12, width: 44, height: 44)
        rigthItems.frame = CGRect.init(x: ScreenWidth - 54, y: 12, width: 34, height: 44)
        titleLabel.frame = CGRect.init(x: 0, y: 12, width: 180, height: 44)
        titleLabel.centerX = self.centerX
    }
    
}
